<template>
  <div class="container">
     <el-form :model="form" ref="deptForm" :rules="rules">
    <el-form-item label="所属学科" prop="subjectID" v-if="!$route.query.id">
      <el-select v-model="form.subjectID" placeholder="请选择学科">
        <el-option v-for="item in subjects" :key="item.id" :label="item.subjectName" :value="item.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="目录名称" prop="directoryName">
      <el-input v-model.trim="form.directoryName" placeholder="请输入目录名称" autocomplete="off"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="close">取 消</el-button>
    <el-button type="primary" @click="addOrEditFn">确 定</el-button>
  </div>
  </div>
</template>

<script>
import {
  detail as detailAPI,
  add as addDirectorysAPI,
  update as updateDirectorysAPI,
  list as directorysAPI
} from '@/api/hmmm/directorys.js'
import {
  list as subjectsAPI
} from '@/api/hmmm/subjects.js'
export default {
  name: 'DirectorysAdd',
  props: {

    isEdit: {
      type: Boolean,
      required: true
    },
    rowd: {
      type: [Object, Array],
      required: true
    }
  },
  data () {
    const directoryName = (rules, value, callback) => {
      let directoryNameList = this.tableData.filter(item => item.subjectID === this.form.subjectID)
      if (this.isEdit) {
        // 如果是编辑 还是组成同级的数组 只不过需要将自己剔除
        directoryNameList = this.tableData.filter(item => item.subjectID === this.form.subjectID && item.id !== this.form.id)
        // existNameList = this.originList.filter(item => item.pid === this.formData.pid && item.id !== this.formData.id)
      }
      directoryNameList.map(item => item.directoryName).includes(value) ? callback(new Error(value + '已经占用')) : callback()
    }
    return {
      form: {
        subjectID: '',
        directoryName: '',
        id: ''
      },
      tableData: [], // 全部目录列表
      rules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' },
          { validator: directoryName }
        ],
        subjectID: [
          { required: true, message: '请选择学科', trigger: 'blur' }
        ]
      },
      subjects: [],
      subjectQuery: {
        pagesize: 99999
      } // 获取学科列表的数值
    }
  },
  methods: {
    async getsubjectsList () {
      const res = await subjectsAPI(this.subjectQuery)
      this.subjects = res.data.items
    },
    // 关闭弹窗
    close () {
      this.$refs.deptForm.resetFields()
      this.$emit('close')
    },
    // 添加
    async confirmAdd () {
      if (this.$route.query.id) {
        this.form.subjectID = this.$route.query.id * 1
      }
      const res = await addDirectorysAPI(this.form)
      if (res.status === 200) {
        this.$message.success('操作成功')
      }
      this.$refs.deptForm.resetFields()
      this.$emit('closeDialog')
    },
    // 编辑
    async confirmUpdate () {
      if (this.$route.query.id) {
        this.form.subjectID = this.$route.query.id * 1
      }
      const formres = await detailAPI(this.rowd)
      this.form.id = this.rowd.id
      this.form.subjectID = formres.data.subjectID
      const res = await updateDirectorysAPI(this.form)
      if (res.data.success === true) {
        this.$message.success('修改成功')
      }
      this.$refs.deptForm.resetFields()
      this.$emit('closeDialog')
    },
    // 点击确定按钮
    addOrEditFn () {
      this.$refs.deptForm.validate(valid => {
        if (!valid) return
        // 如果isEdit为true时候调用编辑方法 否则添加
        this.isEdit ? this.confirmUpdate() : this.confirmAdd()
      })
    },
    // 数据回显
    async getdirectorys () {
      if (this.isEdit) {
        const res = await detailAPI(this.rowd)
        this.form.directoryName = res.data.directoryName
        this.form.subjectID = res.config.params.subjectName
        // this.form = res.data
      }
    },
    async getlist () {
      const res = await directorysAPI({
        pagesize: 999999
      })
      this.tableData = res.data.items
    }

  },
  created () {
    // 获取学科列表
    this.getsubjectsList()
    // 获取当前部门的数据
    this.getdirectorys()
    this.getlist()
  }

}
</script>

<style scoped >
.el-input {
    position: relative;
    font-size: 14px;
    display: inline-block;
    width: 80%;
}
.el-select {
    width: 80%;
}
</style>
